package com.ev.k3cloudBusiness.service.impl;

import com.ev.apis.model.DsResultResponse;
import com.ev.asl.constant.AslConstant;
import com.ev.framework.utils.PageUtils;
import com.ev.framework.utils.R;
import com.ev.k3cloud.service.kingdeeMiddlewareService;
import com.ev.k3cloudBusiness.service.K3SalesStoreHouseService;
import com.ev.k3cloudBusiness.vo.*;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 姚家乐
 * @since 2021/8/3 10:13
 */
@Slf4j
@Service
public class K3SalesStoreHouseServiceImpl implements K3SalesStoreHouseService {

    @Autowired
    private kingdeeMiddlewareService kingdeeMiddlewareService;

    private K3DeeSyncVO<List<SalesStorehouseVo>> syncVO(String yearDate){
        Map<String,String> param = new HashMap<>();
        param.put("yearDate",yearDate);
        String salesStorehouseAll = kingdeeMiddlewareService.SalesStorehouseAll(param);
        return new Gson().fromJson(salesStorehouseAll,
                new TypeToken<K3DeeSyncVO<List<SalesStorehouseVo>>>() {
                }.getType());
    }

    @Override
    public R salesStoreHouseByProductSync(Map<String,Object> param) {


//        K3DeeSyncVO<List<SalesStorehouseVo>> k3DeeSyncVO = this.syncVO(String.valueOf(param.get("yearDate")));
//        if (!AslConstant.ZERO.equals(k3DeeSyncVO.getCode())) {
//            log.info(k3DeeSyncVO.getMsg());
//            return R.error(k3DeeSyncVO.getMsg());
//        }
//        List<SalesStorehouseVo> salesStorehouseVos = k3DeeSyncVO.getData();
//
//        //产品季度报表数据
//        Set<Long> productIds = salesStorehouseVos.stream().map(SalesStorehouseVo::getProductId)
//            .collect(Collectors.toSet());
        List<SalesStorehouseByProductVo> byProductVos = new ArrayList<>();
        SalesStorehouseByProductVo byProductVo;
//        int i = 1;
//        for (Long productId : productIds) {
//            List<SalesStorehouseVo> vos = salesStorehouseVos.stream()
//                .filter(e -> productId.equals(e.getProductId())).collect(Collectors.toList());
//
//            byProductVo = new SalesStorehouseByProductVo();
//            byProductVo.setIndex(i);
//
//            i++;
//            BigDecimal one = BigDecimal.ZERO;
//            BigDecimal two = BigDecimal.ZERO;
//            BigDecimal three = BigDecimal.ZERO;
//            BigDecimal four = BigDecimal.ZERO;
//            for (SalesStorehouseVo vo : vos) {
//                Date voDate = vo.getCreateDate();
//
//                Calendar ca = Calendar.getInstance();
//                ca.setTime(voDate);
//                int month = ca.get(Calendar.MONTH);
//                if (1 <= month && month <= 3) {
//                    one = one.add(vo.getQty());
//                }
//                if (4 <= month && month <= 6) {
//                    two = two.add(vo.getQty());
//                }
//                if (7 <= month && month <= 9) {
//                    three = three.add(vo.getQty());
//                }
//                if (10 <= month && month <= 12) {
//                    four = four.add(vo.getQty());
//                }
//            }
//            byProductVo.setProductName(vos.get(0).getProductName());
//            byProductVo.setProductType(vos.get(0).getProductType());
//            byProductVo.setOneQty(one);
//            byProductVo.setTwoQty(two);
//            byProductVo.setThreeQty(three);
//            byProductVo.setFourQty(four);
//            byProductVo.setQty(one.add(two).add(three).add(four));
//            BigDecimal[] decimals = {one,two,three,four};
//            byProductVo.setQtyList(decimals);
//            byProductVos.add(byProductVo);
//        }
        byProductVo = new SalesStorehouseByProductVo();
        byProductVo.create1();
        byProductVos.add(byProductVo);

        SalesStorehouseByProductVo byProductVo2 = new SalesStorehouseByProductVo();
        byProductVo2.create2();
        byProductVos.add(byProductVo2);

        SalesStorehouseByProductVo byProductVo3 = new SalesStorehouseByProductVo();
        byProductVo3.create3();
        byProductVos.add(byProductVo3);

        SalesStorehouseByProductVo byProductVo4 = new SalesStorehouseByProductVo();
        byProductVo4.create4();
        byProductVos.add(byProductVo4);

        SalesStorehouseByProductVo byProductVo5 = new SalesStorehouseByProductVo();
        byProductVo5.create5();
        byProductVos.add(byProductVo5);

        SalesStorehouseByProductVo byProductVo6 = new SalesStorehouseByProductVo();
        byProductVo6.create6();
        byProductVos.add(byProductVo6);

        SalesStorehouseByProductVo byProductVo7 = new SalesStorehouseByProductVo();
        byProductVo7.create7();
        byProductVos.add(byProductVo7);

        SalesStorehouseByProductVo byProductVo8 = new SalesStorehouseByProductVo();
        byProductVo8.create8();
        byProductVos.add(byProductVo8);

        SalesStorehouseByProductVo byProductVo9 = new SalesStorehouseByProductVo();
        byProductVo9.create9();
        byProductVos.add(byProductVo9);

        SalesStorehouseByProductVo byProductVo10 = new SalesStorehouseByProductVo();
        byProductVo10.create10();
        byProductVos.add(byProductVo10);

        SalesStorehouseByProductVo byProductVo11 = new SalesStorehouseByProductVo();
        byProductVo11.create11();
        byProductVos.add(byProductVo11);

        SalesStorehouseByProductVo byProductVo12 = new SalesStorehouseByProductVo();
        byProductVo12.create12();
        byProductVos.add(byProductVo12);

        SalesStorehouseByProductVo byProductVo13 = new SalesStorehouseByProductVo();
        byProductVo13.create13();
        byProductVos.add(byProductVo13);

        SalesStorehouseByProductVo byProductVo14 = new SalesStorehouseByProductVo();
        byProductVo14.create14();
        byProductVos.add(byProductVo14);

        SalesStorehouseByProductVo byProductVo15 = new SalesStorehouseByProductVo();
        byProductVo15.create15();
        byProductVos.add(byProductVo15);

        SalesStorehouseByProductVo byProductVo16 = new SalesStorehouseByProductVo();
        byProductVo16.create16();
        byProductVos.add(byProductVo16);

        SalesStorehouseByProductVo byProductVo17 = new SalesStorehouseByProductVo();
        byProductVo17.create17();
        byProductVos.add(byProductVo17);

        SalesStorehouseByProductVo byProductVo18 = new SalesStorehouseByProductVo();
        byProductVo18.create18();
        byProductVos.add(byProductVo18);

        SalesStorehouseByProductVo byProductVo19 = new SalesStorehouseByProductVo();
        byProductVo19.create19();
        byProductVos.add(byProductVo19);

        SalesStorehouseByProductVo byProductVo20 = new SalesStorehouseByProductVo();
        byProductVo20.create20();
        byProductVos.add(byProductVo20);
        List<SalesStorehouseByProductVo> data = new ArrayList<>();
        if (!CollectionUtils.isEmpty(byProductVos)){
            data = PageUtils.startPage(byProductVos,Integer.parseInt(String.valueOf(param.get("pageno"))),Integer.parseInt(String.valueOf(param.get("pagesize"))));
        }
        Map<String, Object> result = new HashMap<>();
        result.put("byProduct", data);
        result.put("total",byProductVos.size());
        return R.ok(result);
    }

    @Override
    public R salesStoreHouseByClientSync(Map<String,Object> param) {
//        K3DeeSyncVO<List<SalesStorehouseVo>> k3DeeSyncVO = this.syncVO(String.valueOf(param.get("yearDate")));
//
//        if (!AslConstant.ZERO.equals(k3DeeSyncVO.getCode())) {
//            log.info(k3DeeSyncVO.getMsg());
//            return R.error(k3DeeSyncVO.getMsg());
//        }
//        List<SalesStorehouseVo> salesStorehouseVos = k3DeeSyncVO.getData();
//
//        //客户季度报表数据
//        Set<Long> clientIds = salesStorehouseVos.stream().map(SalesStorehouseVo::getClientId)
//                .collect(Collectors.toSet());
//        BigDecimal totalAmount = BigDecimal.ZERO;
//        for (SalesStorehouseVo vo : salesStorehouseVos) {
//            totalAmount = totalAmount.add(vo.getAmount());
//        }
        List<SalesStorehouseByClientVo> byClientVos = new ArrayList<>();
        SalesStorehouseByClientVo byClientVo;
//        for (Long clientId : clientIds) {
//            List<SalesStorehouseVo> vos = salesStorehouseVos.stream()
//                    .filter(e -> clientId.equals(e.getClientId())).collect(Collectors.toList());
//            byClientVo = new SalesStorehouseByClientVo();
//            BigDecimal one = BigDecimal.ZERO;
//            BigDecimal two = BigDecimal.ZERO;
//            BigDecimal three = BigDecimal.ZERO;
//            BigDecimal four = BigDecimal.ZERO;
//            for (SalesStorehouseVo vo : vos) {
//                Date voDate = vo.getCreateDate();
//
//                Calendar ca = Calendar.getInstance();
//                ca.setTime(voDate);
//                int month = ca.get(Calendar.MONTH);
//                if (1 <= month && month <= 3) {
//                    one = one.add(vo.getAmount());
//                }
//                if (4 <= month && month <= 6) {
//                    two = two.add(vo.getAmount());
//                }
//                if (7 <= month && month <= 9) {
//                    three = three.add(vo.getAmount());
//                }
//                if (10 <= month && month <= 12) {
//                    four = four.add(vo.getAmount());
//                }
//            }
//
//            byClientVo.setClientName(vos.get(0).getClientName());
//            byClientVo.setOneAmount(one);
//            byClientVo.setTwoAmount(two);
//            byClientVo.setThreeAmount(three);
//            byClientVo.setFourAmount(four);
//            BigDecimal amount = one.add(two).add(three).add(four);
//            byClientVo.setAmount(amount);
//            byClientVo.setAmountList(new BigDecimal[]{one,two,three,four});
//
//            BigDecimal proportion = amount.divide(totalAmount, 4, BigDecimal.ROUND_HALF_UP);
//            NumberFormat percent = NumberFormat.getPercentInstance();
//            percent.setMaximumFractionDigits(2);
//            byClientVo.setProportion(percent.format(proportion.doubleValue()));
//            byClientVos.add(byClientVo);
//        }

//        测试start

        byClientVo = new SalesStorehouseByClientVo();
        byClientVo.create1();
        byClientVos.add(byClientVo);

        SalesStorehouseByClientVo byClientVo2 = new SalesStorehouseByClientVo();
        byClientVo2.create2();
        byClientVos.add(byClientVo2);

        SalesStorehouseByClientVo byClientVo3 = new SalesStorehouseByClientVo();
        byClientVo3.create3();
        byClientVos.add(byClientVo3);

        SalesStorehouseByClientVo byClientVo4 = new SalesStorehouseByClientVo();
        byClientVo4.create4();
        byClientVos.add(byClientVo4);

        SalesStorehouseByClientVo byClientVo5 = new SalesStorehouseByClientVo();
        byClientVo5.create5();
        byClientVos.add(byClientVo5);

        SalesStorehouseByClientVo byClientVo6 = new SalesStorehouseByClientVo();
        byClientVo6.create6();
        byClientVos.add(byClientVo6);

        SalesStorehouseByClientVo byClientVo7 = new SalesStorehouseByClientVo();
        byClientVo7.create7();
        byClientVos.add(byClientVo7);

        SalesStorehouseByClientVo byClientVo8 = new SalesStorehouseByClientVo();
        byClientVo8.create8();
        byClientVos.add(byClientVo8);

        SalesStorehouseByClientVo byClientVo9 = new SalesStorehouseByClientVo();
        byClientVo9.create9();
        byClientVos.add(byClientVo9);

        SalesStorehouseByClientVo byClientVo10 = new SalesStorehouseByClientVo();
        byClientVo10.create10();
        byClientVos.add(byClientVo10);

        SalesStorehouseByClientVo byClientVo11 = new SalesStorehouseByClientVo();
        byClientVo11.create11();
        byClientVos.add(byClientVo11);

        SalesStorehouseByClientVo byClientVo12 = new SalesStorehouseByClientVo();
        byClientVo12.create12();
        byClientVos.add(byClientVo12);

        SalesStorehouseByClientVo byClientVo13 = new SalesStorehouseByClientVo();
        byClientVo13.create13();
        byClientVos.add(byClientVo13);

        SalesStorehouseByClientVo byClientVo14 = new SalesStorehouseByClientVo();
        byClientVo14.create14();
        byClientVos.add(byClientVo14);

        SalesStorehouseByClientVo byClientVo15 = new SalesStorehouseByClientVo();
        byClientVo15.create15();
        byClientVos.add(byClientVo15);

        SalesStorehouseByClientVo byClientVo16 = new SalesStorehouseByClientVo();
        byClientVo16.create16();
        byClientVos.add(byClientVo16);

        SalesStorehouseByClientVo byClientVo17 = new SalesStorehouseByClientVo();
        byClientVo17.create17();
        byClientVos.add(byClientVo17);

        SalesStorehouseByClientVo byClientVo18 = new SalesStorehouseByClientVo();
        byClientVo18.create18();
        byClientVos.add(byClientVo18);

        SalesStorehouseByClientVo byClientVo19 = new SalesStorehouseByClientVo();
        byClientVo19.create19();
        byClientVos.add(byClientVo19);

        SalesStorehouseByClientVo byClientVo20 = new SalesStorehouseByClientVo();
        byClientVo20.create20();
        byClientVos.add(byClientVo20);
//        测试end

        List<SalesStorehouseByClientVo> data = new ArrayList<>();
        if (!CollectionUtils.isEmpty(byClientVos)){
            data = PageUtils.startPage(byClientVos,Integer.parseInt(String.valueOf(param.get("pageno"))),Integer.parseInt(String.valueOf(param.get("pagesize"))));
        }
        Map<String, Object> result = new HashMap<>();
        result.put("byClient", data);
        result.put("total", byClientVos.size());

        return R.ok(result);
    }
}
